#!/usr/bin/python  
#=00 Signals
import pygst; pygst.require("0.10")  #--
import gst; import gobject  #--
class playback:  
    def __init__(self, filename):  
        self.pipeline = gst.Pipeline('player')  
#=03 Get Bus from Pipeline
        # Get the message bus                 #03
        self.bus = self.pipeline.get_bus()    #03
        self.bus.add_watch(self.message)      #03
        filesrc = gst.element_factory_make("filesrc", "input")  
        filesrc.set_property('location', filename)  
        converter = gst.element_factory_make("mad", "converter")  
        output = gst.element_factory_make("alsasink", "output")  
        output.set_property('device', 'plughw:1')  
#=01 Laziness is a virtue
        # Add them all to the pipeline with one call.      #01
        self.pipeline.add(filesrc, converter, output)      #01
        # Link them all together in one call.              #01
        gst.element_link_many(filesrc, converter, output)  #01
        self.pipeline.set_state(gst.STATE_PLAYING)  
#=04 The Bus callback
    def message(self, bus, message):            #04
#=06 Check for EOS
        # Check for an 'End Of Stream' message  #06
        if message.type == gst.MESSAGE_EOS:     #06
            print "Got EOS, quitting mainloop"  #06
            self.mainloop.quit()                #06
#=05 Callback Return Values
        # We want to continue receiving messages, so return True  #05
        return True                                               #05
  
#=02 Mainloop has moved
    def run(self):                          #02
        self.mainloop = gobject.MainLoop()  #02
        self.mainloop.run()                 #02
    def quit(self, *args):                  #02
        self.mainloop.quit()                #02
  
if __name__ == "__main__":  
    import sys  
    player=playback(sys.argv[1])  
    player.run()                            #02
